package com.ithuameng.admin.app.system.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ithuameng.admin.app.system.model.Menu;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Menu mapper
 *
 * @author ithuameng
 */
public interface MenuMapper extends BaseMapper<Menu> {

    /**
     * 通过用户编号获取该用户所有权限
     *
     * @param userId 用户编号
     * @return 用户所有权限列表
     */
    @Select({
            "<script>",
            "SELECT DISTINCT m.id, m.name, m.parent_name, m.parent_id, m.path, m.component, m.query, m.frame, m.display, m.type, m.icon, m.perms, m.seq, m.status, m.create_time, m.create_by, m.update_time, m.update_by FROM t_menu m",
            "LEFT JOIN t_role_menu rm  ON rm.menu_id = m.id ",
            "LEFT JOIN t_user_role ur ON ur.role_id = rm.role_id",
            "WHERE m.deleted='N' and ur.user_id=#{userId}",
            "<if test=\"name != null and name !=''\">and m.title LIKE CONCAT('%',#{name},'%') </if>",
            "ORDER BY m.parent_id ASC, m.seq ASC",
            "</script>"
    })
    List<Menu> listByUserId(@Param("userId") Long userId, @Param("name") String name, @Param("status") String status);

    /**
     * 根据用户ID查询权限
     *
     * @param userId 用户ID
     * @return 权限列表
     */
    @Select({
            "<script>", "SELECT DISTINCT m.perms FROM t_menu m", "LEFT JOIN t_role_menu rm ON m.id = rm.menu_id",
            "LEFT JOIN t_user_role ur ON rm.role_id = ur.role_id",
            "LEFT JOIN t_role r ON (r.id = ur.role_id and r.deleted='N')",
            "WHERE m.deleted='N' and ur.user_id=#{userId}",
            "</script>"
    })
    List<String> selectMenuPermsByUserId(@Param("userId") Long userId);

    /**
     * 根据用户ID查询菜单
     *
     * @return 菜单列表
     */
    @Select({
            "<script>",
            "SELECT DISTINCT m.id, m.name, m.parent_name, m.parent_id, m.path, m.component, m.query, m.frame, m.display, m.type, m.icon, m.perms, m.seq, m.status, m.create_time, m.create_by, m.update_time, m.update_by FROM t_menu m",
            "WHERE m.deleted='N' ORDER BY m.parent_id, m.seq",
            "</script>"
    })
    List<Menu> selectMenuTreeAll();

    /**
     * 根据用户ID查询菜单
     *
     * @param userId 用户ID
     * @return 菜单列表
     */
    @Select({
            "<script>",
            "SELECT DISTINCT m.id, m.name, m.parent_name, m.parent_id, m.path, m.component, m.query, m.frame, m.display, m.type, m.icon, m.perms, m.seq, m.status, m.create_time, m.create_by, m.update_time, m.update_by FROM t_menu m",
            "left join t_role_menu rm on m.id = rm.menu_id", "left join t_user_role ur on rm.role_id = ur.role_id",
            "left join t_role r on (ur.role_id = r.id and r.deleted='N')",
            "left join t_user u on (ur.user_id = u.id and u.deleted='N')",
            "WHERE m.deleted='N' and u.id=#{userId}",
            "ORDER BY m.parent_id, m.seq",
            "</script>"
    })
    List<Menu> selectMenuTreeByUserId(Long userId);

    /**
     * 根据角色ID查询菜单
     *
     * @param roleId 角色ID
     * @return 菜单ID集合
     */
    @Select({
            "<script>",
            "SELECT m.id FROM t_menu m",
            "left join t_role_menu rm on m.id = rm.menu_id",
            "where m.deleted='N' and rm.role_id = #{roleId}",
            "order by m.parent_id, m.seq",
            "</script>"
    })
    List<Long> selectMenuListByRoleId(Long roleId);
}
